package com.jspxcms.core.domain;

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.jspxcms.common.util.ScoreUtil;
import com.jspxcms.core.service.impl.ScoreServiceImpl;
import org.hibernate.validator.constraints.Length;

/**
 * UserDetail
 *
 * @author liufang
 */
@Entity
@Table(name = "cms_user_detail")
public class UserDetail implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 登录错误间隔时间
     */
    public static final int LOGIN_ERROR_MILLIS = 30 * 60 * 1000;

    /**
     * 是否需要验证码
     * <p/>
     * 错误次数超过3次，且在半小时内。
     *
     * @return
     */
    @Transient
    public boolean isCaptchaRequired() {
        if (getLoginErrorCount() > 3
                && System.currentTimeMillis() - getLoginErrorDate().getTime() < LOGIN_ERROR_MILLIS) {
            return true;
        } else {
            return false;
        }
    }

    @Transient
    public void applyDefaultValue() {
        if (getCreationDate() == null) {
            setCreationDate(new Timestamp(System.currentTimeMillis()));
        }
        if (getCreationIp() == null) {
            setCreationIp("127.0.0.1");
        }
        if (getLoginErrorCount() == null) {
            setLoginErrorCount(0);
        }
        if (getLogins() == null) {
            setLogins(0);
        }
    }

    private Integer id;
    private User user;
    private Date validationDate;
    private String validationValue;
    private Date loginErrorDate;
    private Integer loginErrorCount;
    private Date prevLoginDate;
    private String prevLoginIp;
    private Date lastLoginDate;
    private String lastLoginIp;
    private Date creationDate;
    private String creationIp;
    private Integer logins;

    private String avatar;
    private String bio;
    private String comeFrom;
    private String qq;
    private String msn;
    private String weixin;
    private String hospital;
    private Integer areaid;

    private Integer score;
    private Integer scoreFavorite;
    private Integer scoreStudy;
    private Integer scoreLevel;

    @Column(name = "f_score")
    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    @Column(name = "f_score_favorite")
    public Integer getScoreFavorite() {
        return scoreFavorite;
    }

    public void setScoreFavorite(Integer scoreFavorite) {
        this.scoreFavorite = scoreFavorite;
    }

    @Column(name = "f_score_study")
    public Integer getScoreStudy() {
        return scoreStudy;
    }

    public void setScoreStudy(Integer scoreStudy) {
        this.scoreStudy = scoreStudy;
    }


    @Column(name = "f_score_level")
    public Integer getScoreLevel() {
        return scoreLevel;
    }

    public void setScoreLevel(Integer scoreLevel) {
        this.scoreLevel = scoreLevel;
    }


    @Id
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @MapsId
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "f_user_id")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "f_validation_date", length = 19)
    public Date getValidationDate() {
        return validationDate;
    }

    public void setValidationDate(Date validationDate) {
        this.validationDate = validationDate;
    }

    @Length(max = 255)
    @Column(name = "f_validation_value", length = 255)
    public String getValidationValue() {
        return validationValue;
    }

    public void setValidationValue(String validationValue) {
        this.validationValue = validationValue;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "f_login_error_date", length = 19)
    public Date getLoginErrorDate() {
        return loginErrorDate;
    }

    public void setLoginErrorDate(Date loginErrorDate) {
        this.loginErrorDate = loginErrorDate;
    }

    @Column(name = "f_login_error_count", nullable = false)
    public Integer getLoginErrorCount() {
        return loginErrorCount;
    }

    public void setLoginErrorCount(Integer loginErrorCount) {
        this.loginErrorCount = loginErrorCount;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "f_prev_login_date", length = 19)
    public Date getPrevLoginDate() {
        return this.prevLoginDate;
    }

    public void setPrevLoginDate(Date prevLoginDate) {
        this.prevLoginDate = prevLoginDate;
    }

    @Length(max = 100)
    @Column(name = "f_prev_login_ip", length = 100)
    public String getPrevLoginIp() {
        return this.prevLoginIp;
    }

    public void setPrevLoginIp(String prevLoginIp) {
        this.prevLoginIp = prevLoginIp;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "f_last_login_date", length = 19)
    public Date getLastLoginDate() {
        return this.lastLoginDate;
    }

    public void setLastLoginDate(Date lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }

    @Length(max = 100)
    @Column(name = "f_last_login_ip", length = 100)
    public String getLastLoginIp() {
        return this.lastLoginIp;
    }

    public void setLastLoginIp(String lastLoginIp) {
        this.lastLoginIp = lastLoginIp;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "f_creation_date", nullable = false, length = 19)
    public Date getCreationDate() {
        return this.creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    @Length(max = 100)
    @Column(name = "f_creation_ip", length = 100)
    public String getCreationIp() {
        return this.creationIp;
    }

    public void setCreationIp(String creationIp) {
        this.creationIp = creationIp;
    }

    @Column(name = "f_logins", nullable = false)
    public Integer getLogins() {
        return this.logins;
    }

    public void setLogins(Integer logins) {
        this.logins = logins;
    }

    @Length(max = 255)
    @Column(name = "f_avatar")
    public String getAvatar() {
        return this.avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    @Column(name = "f_bio")
    public String getBio() {
        return this.bio;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    @Length(max = 100)
    @Column(name = "f_come_from", length = 100)
    public String getComeFrom() {
        return this.comeFrom;
    }

    public void setComeFrom(String comeFrom) {
        this.comeFrom = comeFrom;
    }

    @Length(max = 100)
    @Column(name = "f_qq", length = 100)
    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    @Length(max = 100)
    @Column(name = "f_msn", length = 100)
    public String getMsn() {
        return msn;
    }

    public void setMsn(String msn) {
        this.msn = msn;
    }

    @Length(max = 100)
    @Column(name = "f_weixin", length = 100)
    public String getWeixin() {
        return weixin;
    }

    public void setWeixin(String weixin) {
        this.weixin = weixin;
    }

    @Length(max = 100)
    @Column(name = "f_hospital", length = 100)
    public String getHospital() {
        return this.hospital;
    }

    public void setHospital(String hospital) {
        this.hospital = hospital;
    }

    @Column(name = "f_areaid", nullable = true)
    public Integer getAreaid() {
        return this.areaid;
    }

    public void setAreaid(Integer areaid) {
        this.areaid = areaid;
    }


    @Transient
    public String getLevel() {
        return ScoreUtil.getLevelName(this);
    }
}
